# coding=utf-8
from django.shortcuts import render_to_response
from django.core.exceptions import ObjectDoesNotExist
from apps.person.models import Person
from apps.research.views import head_group

IDENTITY_TRANSFORM = {
    'advisory-committee': 'AC',
    'technical-committee': 'TC',
    'faculty': 'FT',
    'administrative-staff': 'AS',
    'technical-staff': 'TS',
    'interns': 'IT',
    'alumni': 'AU'}

def community(request, lang, department):
#   语言检验
    if lang != 'en' and lang != 'zh':
        return render_to_response('404.html', {'error': 'Unkown language error'})
#   部门转换，长->短
    try:
        identity = IDENTITY_TRANSFORM[department]
    except KeyError:
        return render_to_response('404.html', {'error': 'Invalid Department'})
    except:
        return render_to_response('404.html')   #   Unkown Error
#   过滤identity
    persons = list(Person.objects.filter(person_identity=identity))
    for person in persons:
        if person.person_image == '':
            if person.person_gender == 'M':
                person.person_image = 'uploads/persons/male.jpg'
            else:
                person.person_image = 'uploads/persons/female.jpg'
    return render_to_response('community.html', {
        'current_item': 'community',
        'lang': lang,
        'url_en': '/en/community/'+department,
        'url_zh': '/zh/community/'+department,
        'head_group_list': head_group(),
        'current_department': identity,
        'persons': persons})
